package ModelLayer;
import java.util.*;

/**
 * Write a description of class Lease here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Sale
{
    private int saleId;
    private String saleDate;
    private ArrayList<SalesLine> salesLine;
    private ArrayList<Product> productList;
    private Customer customer;

    public Sale(String saleDate, int saleId)
    {
        this.saleDate = saleDate;
        this.saleId = saleId;
        salesLine = new ArrayList<SalesLine>();
        productList = new ArrayList<Product>();
        
    }

    //SETTERS

    public void setLeaseDate(String newLeaseDate)
    {
        saleDate = newLeaseDate;
    }

    public void setSaleId(int newId)
    {
        saleId = newId;
    }

    public void addSalesLine(SalesLine s)
    {
        salesLine.add(s);
    }

    public boolean addProductToList(Product p, int quantity)
    {
        if(quantity <= p.getQuantity())
        {
            Product p1 = p;
            p.setQuantity(p.getQuantity() - quantity);
            p1.setQuantity(quantity);
            productList.add(p1); 
            return true;
        }
        else
        {
            return false;
        }
    }

            
            
    public void deleteProductFromList(Product p)
    {
        productList.remove(p);
    }

    //GETTERS

    public String getLeaseDate()
    {
        return saleDate;
    }

    public int getSaleId()
    {
        return saleId;
    }

    //==========================================

    
    public Product findProductInListByBarCode(int barcode)
    {
        Product pObject = null;
        int index = 0;
        boolean found = false;
        while(index < productList.size() && !found)
        {

            if(pObject.getBarCode() == barcode)
            {
                pObject = productList.get(index); 
                found = true;
            }
            else
            {
                index++;
            }
        }//end while
        return productList.get(index);
    }

    public double getTotalPrice(/*boolean delivery*/)
    {
        double totalPrice=0;
        for (int i=0; i<salesLine.size(); i++)
        {
            totalPrice += salesLine.get(i).getProduct().getPrice() * salesLine.get(i).getSalesQuantity() *  customer.calculateDiscount();
        } 

        //if(delivery)
        //{
        //    totalPrice+= deliveryPrice;
        // }

        return totalPrice;
    }
}
